﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class Tab_MessageDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="MessageID">标识列ID</param>
		public bool Exists(int MessageID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [Tab_Message] where MessageID=@messageid ");
			htSyn.Add("@messageid", MessageID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<Tab_Message> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [MessageID],[CaseID],[UserName],[UserConnect],[MessageTitle],[MessageInfo],[MessageDate] FROM [Tab_Message]");
			 return getTab_MessageBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="MessageID">标识列ID</param>
		/// <returns></returns>
		public Tab_Message GetModel(int messageid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [MessageID],[CaseID],[UserName],[UserConnect],[MessageTitle],[MessageInfo],[MessageDate] FROM [Tab_Message]");
			strSql.Append(" where MessageID=@messageid ");
			htSyn.Add("@messageid", messageid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			Tab_Message tab_messageModel = new Tab_Message();
			if (dt.Rows.Count > 0)
			{
				tab_messageModel.caseID=Convert.ToInt32(dt.Rows[0]["CaseID"]);
				tab_messageModel.userName=dt.Rows[0]["UserName"].ToString();
				tab_messageModel.userConnect=dt.Rows[0]["UserConnect"].ToString();
				tab_messageModel.messageTitle=dt.Rows[0]["MessageTitle"].ToString();
				tab_messageModel.messageInfo=dt.Rows[0]["MessageInfo"].ToString();
				tab_messageModel.messageDate=dt.Rows[0]["MessageDate"].ToString();
			}
			return tab_messageModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="Tab_Message">实体</param>
		/// <returns></returns>
		public bool Update(Tab_Message tab_messageModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [Tab_Message] set ");
			strSql.Append("[CaseID]=@caseid,");
			strSql.Append("[UserName]=@username,");
			strSql.Append("[UserConnect]=@userconnect,");
			strSql.Append("[MessageTitle]=@messagetitle,");
			strSql.Append("[MessageInfo]=@messageinfo,");
			strSql.Append("[MessageDate]=@messagedate");

			strSql.Append(" where MessageID=@messageid ");
			htSyn.Add("@messageid", tab_messageModel.messageID);
			htSyn.Add("@caseid", tab_messageModel.caseID);
			htSyn.Add("@username", tab_messageModel.userName);
			htSyn.Add("@userconnect", tab_messageModel.userConnect);
			htSyn.Add("@messagetitle", tab_messageModel.messageTitle);
			htSyn.Add("@messageinfo", tab_messageModel.messageInfo);
			htSyn.Add("@messagedate", tab_messageModel.messageDate);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="MessageID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int MessageID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [Tab_Message] where MessageID=@messageid");
			htSyn.Add("messageid", MessageID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="Tab_Message">实体</param>
		/// <returns></returns>
		public bool Insert(Tab_Message tab_messageModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [Tab_Message]([CaseID],[UserName],[UserConnect],[MessageTitle],[MessageInfo],[MessageDate]) values (@caseid,@username,@userconnect,@messagetitle,@messageinfo,@messagedate)");
			htSyn.Add("@caseid", tab_messageModel.caseID);
			htSyn.Add("@username", tab_messageModel.userName);
			htSyn.Add("@userconnect", tab_messageModel.userConnect);
			htSyn.Add("@messagetitle", tab_messageModel.messageTitle);
			htSyn.Add("@messageinfo", tab_messageModel.messageInfo);
			htSyn.Add("@messagedate", tab_messageModel.messageDate);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<Tab_Message> getTab_MessageBySql(string strSql)
		{
			IList<Tab_Message> list = new List<Tab_Message>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Tab_Message tab_messageModel = new Tab_Message();
					tab_messageModel.messageID= Convert.ToInt32(dr["MessageID"]);
					tab_messageModel.caseID= Convert.ToInt32(dr["CaseID"]);
					tab_messageModel.userName=dr["UserName"].ToString();
					tab_messageModel.userConnect=dr["UserConnect"].ToString();
					tab_messageModel.messageTitle=dr["MessageTitle"].ToString();
					tab_messageModel.messageInfo=dr["MessageInfo"].ToString();
					tab_messageModel.messageDate=dr["MessageDate"].ToString();
					list.Add(tab_messageModel);
				}
			}
			return list;
		}
		#endregion
	}
}
